home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / net / readline-2.0mg.tar.gz / readline-2.0mg.tar / readline-2.0mg / search.c < prev    next >
C/C++ Source or Header  |  1994-11-20  |  9KB  |  372 lines

  1. /* search.c - code for non-incremental searching in emacs and vi modes. */
  2.  
  3. /* Copyright (C) 1992 Free Software Foundation, Inc.
  4.  
  5.    This file is part of the Readline Library (the Library), a set of
  6.    routines for providing Emacs style line input to programs that ask
  7.    for it.
  8.  
  9.    The Library is free software; you can redistribute it and/or modify
  10.    it under the terms of the GNU General Public License as published by
  11.    the Free Software Foundation; either version 1, or (at your option)
  12.    any later version.
  13.  
  14.    The Library is distributed in the hope that it will be useful, but
  15.    WITHOUT ANY WARRANTY; without even the implied warranty of
  16.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17.    General Public License for more details.
  18.  
  19.    The GNU General Public License is often shipped with GNU software, and
  20.    is generally kept in a file called COPYING or LICENSE.  If you do not
  21.    have a copy of the license, write to the Free Software Foundation,
  22.    675 Mass Ave, Cambridge, MA 02139, USA. */
  23. #define READLINE_LIBRARY
  24.  
  25. #if defined (HAVE_CONFIG_H)
  26. #  include "config.h"
  27. #endif
  28.  
  29. #include <sys/types.h>
  30. #include <stdio.h>
  31.  
  32. #if defined (HAVE_UNISTD_H)
  33. #  include <unistd.h>
  34. #endif
  35.  
  36. #include "memalloc.h"
  37. #include "rldefs.h"
  38. #include "readline.h"
  39. #include "history.h"
  40.  
  41. #define STREQ(a, b)    (((a)[0] == (b)[0]) && (strcmp ((a), (b)) == 0))
  42. #define STREQN(a, b, n)    (((a)[0] == (b)[0]) && (strncmp ((a), (b), (n)) == 0))
  43.  
  44. #define abs(x)        (((x) > 0) ? (x) : -(x))
  45.  
  46. extern char *xmalloc (), *xrealloc ();
  47.  
  48. /* Variables imported from readline.c */
  49. extern int rl_point, rl_end, rl_line_buffer_len;
  50. extern Keymap _rl_keymap;
  51. extern char *rl_prompt;
  52. extern char *rl_line_buffer;
  53. extern HIST_ENTRY *saved_line_for_history;
  54. extern Function *rl_last_func;
  55.  
  56. /* Functions imported from the rest of the library. */
  57. extern int _rl_free_history_entry ();
  58.  
  59. static char *noninc_search_string = (char *) NULL;
  60. static int noninc_history_pos = 0;
  61. static char *prev_line_found = (char *) NULL;
  62.  
  63. /* Search the history list for STRING starting at absolute history position
  64.    POS.  If STRING begins with `^', the search must match STRING at the
  65.    beginning of a history line, otherwise a full substring match is performed
  66.    for STRING.  DIR < 0 means to search backwards through the history list,
  67.    DIR >= 0 means to search forward. */
  68. static int
  69. noninc_search_from_pos (string, pos, dir)
  70.      char *string;
  71.      int pos, dir;
  72. {
  73.   int ret, old;
  74.  
  75.   old = where_history ();
  76.   history_set_pos (pos);
  77.  
  78.   if (*string == '^')
  79.     ret = history_search_prefix (string + 1, dir);
  80.   else
  81.     ret = history_search (string, dir);
  82.  
  83.   if (ret != -1)
  84.     ret = where_history ();
  85.  
  86.   history_set_pos (old);
  87.   return (ret);
  88. }
  89.  
  90. /* Search for a line in the history containing STRING.  If DIR is < 0, the
  91.    search is backwards through previous entries, else through subsequent
  92.    entries. */
  93. static void
  94. noninc_dosearch (string, dir)
  95.      char *string;
  96.      int dir;
  97. {
  98.   int oldpos, pos;
  99.   HIST_ENTRY *entry;
  100.  
  101.   if (string == 0 || *string == 0 || noninc_history_pos < 0)
  102.     {
  103.       ding ();
  104.       return;
  105.     }
  106.  
  107.   pos = noninc_search_from_pos (string, noninc_history_pos + dir, dir);
  108.   if (pos == -1)
  109.     {
  110.       /* Search failed, current history position unchanged. */
  111.       maybe_unsave_line ();
  112.       rl_clear_message ();
  113.       rl_point = 0;
  114.       ding ();
  115.       return;
  116.     }
  117.  
  118.   noninc_history_pos = pos;
  119.  
  120.   oldpos = where_history ();
  121.   history_set_pos (noninc_history_pos);
  122.   entry = current_history ();
  123.   history_set_pos (oldpos);
  124.  
  125.   {
  126.     int line_len;
  127.  
  128.     line_len = strlen (entry->line);
  129.     if (line_len >= rl_line_buffer_len)
  130.       rl_extend_line_buffer (line_len);
  131.     strcpy (rl_line_buffer, entry->line);
  132.   }
  133.  
  134.   rl_undo_list = (UNDO_LIST *)entry->data;
  135.   rl_end = strlen (rl_line_buffer);
  136.   rl_point = 0;
  137.   rl_clear_message ();
  138.  
  139.   if (saved_line_for_history)
  140.     _rl_free_history_entry (saved_line_for_history);
  141.   saved_line_for_history = (HIST_ENTRY *)NULL;
  142. }
  143.  
  144. /* Search non-interactively through the history list.  DIR < 0 means to
  145.    search backwards through the history of previous commands; otherwise
  146.    the search is for commands subsequent to the current position in the
  147.    history list.  PCHAR is the character to use for prompting when reading
  148.    the search string; if not specified (0), it defaults to `:'. */
  149. static void
  150. noninc_search (dir, pchar)
  151.      int dir;
  152.      int pchar;
  153. {
  154.   int saved_point, c, pmtlen;
  155.   char *p;
  156.  
  157.   maybe_save_line ();
  158.   saved_point = rl_point;
  159.  
  160.   /* Use the line buffer to read the search string. */
  161.   rl_line_buffer[0] = 0;
  162.   rl_end = rl_point = 0;
  163.  
  164.   /* XXX - this needs fixing to work with the prompt expansion stuff - XXX */
  165.   pmtlen = (rl_prompt && *rl_prompt) ? strlen (rl_prompt) : 0;
  166.   p = xmalloc (2 + pmtlen);
  167.   if (pmtlen)
  168.     strcpy (p, rl_prompt);
  169.   p[pmtlen] = pchar ? pchar : ':';
  170.   p[pmtlen + 1]  = '\0';
  171.  
  172.   rl_message (p, 0, 0);
  173.   free (p);
  174.  
  175.   /* Read the search string. */
  176.   while (c = rl_read_key ())
  177.     {
  178.       switch (c)
  179.     {
  180.     case CTRL('H'):
  181.     case RUBOUT:
  182.       if (rl_point == 0)
  183.         {
  184.           maybe_unsave_line ();
  185.           rl_clear_message ();
  186.           rl_point = saved_point;
  187.           return;
  188.         }
  189.       rl_rubout (1);
  190.       break;
  191.  
  192.     case CTRL('W'):
  193.       rl_unix_word_rubout ();
  194.       break;
  195.  
  196.     case CTRL('U'):
  197.       rl_unix_line_discard ();
  198.       break;
  199.  
  200.     case RETURN:
  201.     case NEWLINE:
  202.       goto dosearch;
  203.       /* NOTREACHED */
  204.       break;
  205.  
  206.     case CTRL('C'):
  207.     case CTRL('G'):
  208.       maybe_unsave_line ();
  209.       rl_clear_message ();
  210.       rl_point = saved_point;
  211.       ding ();
  212.       return;
  213.  
  214.     default:
  215.       rl_insert (1, c);
  216.       break;
  217.     }
  218.       rl_redisplay ();
  219.     }
  220.  
  221.  dosearch:
  222.   /* If rl_point == 0, we want to re-use the previous search string and
  223.      start from the saved history position.  If there's no previous search
  224.      string, punt. */
  225.   if (rl_point == 0)
  226.     {
  227.       if (!noninc_search_string)
  228.     {
  229.       ding ();
  230.       return;
  231.     }
  232.     }
  233.   else
  234.     {
  235.       /* We want to start the search from the current history position. */
  236.       noninc_history_pos = where_history ();
  237.       if (noninc_search_string)
  238.     free (noninc_search_string);
  239.       noninc_search_string = savestring (rl_line_buffer);
  240.     }
  241.  
  242.   noninc_dosearch (noninc_search_string, dir);
  243. }
  244.  
  245. /* Search forward through the history list for a string.  If the vi-mode
  246.    code calls this, KEY will be `?'. */
  247. rl_noninc_forward_search (count, key)
  248.      int count, key;
  249. {
  250.   if (key == '?')
  251.     noninc_search (1, '?');
  252.   else
  253.     noninc_search (1, 0);
  254.   return 0;
  255. }
  256.  
  257. /* Reverse search the history list for a string.  If the vi-mode code
  258.    calls this, KEY will be `/'. */
  259. rl_noninc_reverse_search (count, key)
  260.      int count, key;
  261. {
  262.   if (key == '/')
  263.     noninc_search (-1, '/');
  264.   else
  265.     noninc_search (-1, 0);
  266.   return 0;
  267. }
  268.  
  269. /* Search forward through the history list for the last string searched
  270.    for.  If there is no saved search string, abort. */
  271. rl_noninc_forward_search_again (count, key)
  272.      int count, key;
  273. {
  274.   if (!noninc_search_string)
  275.     {
  276.       ding ();
  277.       return (-1);
  278.     }
  279.   noninc_dosearch (noninc_search_string, 1);
  280.   return 0;
  281. }
  282.  
  283. /* Reverse search in the history list for the last string searched
  284.    for.  If there is no saved search string, abort. */
  285. rl_noninc_reverse_search_again (count, key)
  286.      int count, key;
  287. {
  288.   if (!noninc_search_string)
  289.     {
  290.       ding ();
  291.       return (-1);
  292.     }
  293.   noninc_dosearch (noninc_search_string, -1);
  294.   return 0;
  295. }
  296.  
  297. static int
  298. rl_history_search_internal (count, direction)
  299.      int count, direction;
  300. {
  301.   HIST_ENTRY *temp, *old_temp;
  302.   int line_len;
  303.  
  304.   maybe_save_line ();
  305.  
  306.   temp = old_temp = (HIST_ENTRY *)NULL;
  307.   while (count)
  308.     {
  309.       temp = (direction < 0) ? previous_history () : next_history ();
  310.       if (!temp)
  311.         break;
  312.       if (STREQN (rl_line_buffer, temp->line, rl_point))
  313.     {
  314.       /* Don't find multiple instances of the same line. */
  315.       if (prev_line_found && STREQ (prev_line_found, temp->line))
  316.         continue;
  317.           if (direction < 0)
  318.             old_temp = temp;
  319.           prev_line_found = temp->line;
  320.           count--;
  321.     }
  322.     }
  323.  
  324.   if (!temp)
  325.     {
  326.       if (direction < 0 && old_temp)
  327.     temp = old_temp;
  328.       else
  329.     {
  330.       maybe_unsave_line ();
  331.       ding ();
  332.       return 1;
  333.     }
  334.     }
  335.  
  336.   line_len = strlen (temp->line);
  337.   if (line_len >= rl_line_buffer_len)
  338.     rl_extend_line_buffer (line_len);
  339.   strcpy (rl_line_buffer, temp->line);
  340.   rl_undo_list = (UNDO_LIST *)temp->data;
  341.   rl_end = line_len;
  342.   return 0;
  343. }
  344.  
  345. /* Search forward in the history for the string of characters
  346.    from the start of the line to rl_point.  This is a non-incremental
  347.    search. */
  348. int
  349. rl_history_search_forward (count, ignore)
  350.      int count, ignore;
  351. {
  352.   if (count == 0)
  353.     return (0);
  354.   if (rl_last_func != rl_history_search_forward)
  355.     prev_line_found = (char *)NULL;
  356.   return (rl_history_search_internal (abs (count), (count > 0) ? 1 : -1));
  357. }
  358.  
  359. /* Search backward through the history for the string of characters
  360.    from the start of the line to rl_point.  This is a non-incremental
  361.    search. */
  362. int
  363. rl_history_search_backward (count, ignore)
  364.      int count, ignore;
  365. {
  366.   if (count == 0)
  367.     return (0);
  368.   if (rl_last_func != rl_history_search_backward)
  369.     prev_line_found = (char *)NULL;
  370.   return (rl_history_search_internal (abs (count), (count > 0) ? -1 : 1));
  371. }
  372.